package Classic150;

import java.util.HashMap;
import java.util.Map;

public class 无重复字符的最长子串 {
    /*
        滑动窗口，hashmap的应用
     */
    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> map = new HashMap<>();
        char[] chars = s.toCharArray();
        int res = 0;
        int start = 0;
        for (int i = 0; i < chars.length; i++) {
            if (map.containsKey(chars[i]) && map.get(chars[i]) >= start) {
                res = Math.max(res, i - start);
                start = map.get(chars[i]) + 1;
            }
            map.put(chars[i], i);
        }
        res = Math.max(res, chars.length - start);
        return res;
    }
}
